package com.xbongbong.saas.help;

import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.pojo.dto.FormDataRepeatDTO;
import com.xbongbong.paas.pojo.vo.FormDataRepeatVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.common.pojo.dto.RepeatCommonConditionDTO;
import com.xbongbong.saas.enums.business.BomBillEnum;
import com.xbongbong.paas.help.CommonHelp;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;

/**
 * @author 吴峰
 * @date 2021/07/19 9:56
 */
@Component
public class SaasBomBillHelp {

    @Resource
    private CommonHelp commonHelp;

    public FormDataRepeatVO checkRepeatForBomVersion(RepeatCommonConditionDTO repeatCommonConditionDTO, String finishedProductVersion, Long bomId, Long productId, Boolean isSave) throws XbbException {
        // bom清单成品id
        FormDataRepeatDTO formDataRepeatDTO = new FormDataRepeatDTO();
        BeanUtil.copyProperties(repeatCommonConditionDTO, formDataRepeatDTO);
        formDataRepeatDTO.setAttr(BomBillEnum.PRODUCT.getAttr());
        formDataRepeatDTO.setValue(productId);
        // 拼接条件，版本
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
        conditionsEntityExt.setAttr(BomBillEnum.VERSION.getAttr());
        //判断是保存BOM清单时查重还是输入时查重
        if (isSave) {
            conditionsEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
        }else {
            conditionsEntityExt.setSymbol(ConditionEnum.LIKE.getSymbol());
        }
        conditionsEntityExt.setValue(Collections.singletonList(finishedProductVersion));
        conditionsEntityExt.setFieldType(FieldTypeEnum.TEXT.getType());
        return commonHelp.isUniqueByConditions(formDataRepeatDTO, Arrays.asList(conditionsEntityExt));
    }
}
